<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/28
 * Time: 20:31
 */

namespace app\storage\model;


use app\storage\service\MaterielService;
use app\storage\service\SupplierService;
use service\k3\K3ReaderFactory;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class PurchaseModel extends Model
{
    protected $table = 'iwh_purchase';
    protected $pk = 'uuid';

    public function Storage()
    {
        return $this->belongsTo('StorageModel', 'storage_uuid', 'uuid')->bind(['storage_name' => 'name']);
    }

    /**
     * @param $condition
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getPurchase($condition)
    {
        $where = $condition['where'];
        $pageSize = $condition['pageSize'];
        $pageIndex = $condition['pageIndex'];
        $result['total'] = $this->alias('p')->field('p.*,s.name as storage_name')

            ->join(['iwh_storage' => 's'], 's.uuid = p.storage_uuid', 'left')
            ->whereOr('storage_uuid','=',null)
            ->whereOr('storage_uuid','=',"")
            ->whereOr($where)
            ->count();

        $result['row'] = $this->alias('p')->field('p.*,s.name as storage_name,sp.company_name')
            ->join(['iwh_supplier' => 'sp'], 'sp.supplier_id = p.supplier_id', 'left')
            ->join(['iwh_storage' => 's'], 's.uuid = p.storage_uuid', 'left')
            ->page($pageIndex, $pageSize)
            ->order('time desc')
            ->select();

        return $result;
    }



}